const state = {
  all: []
}

const mutations = {
  setAll(state, data) {
    state.all = data
  },

  changeAll(state, id) {
    let product = state.all.find(v => v.id === id)
    product.inventory--
  }
}

const getters = {
  isOutsaled(state) {
    return (id) => {
      let product = state.all.find(v => v.id === id)
      return product.inventory <= 0
    }
  }
}

const actions = {
  loadData({commit}) {
    fetch('/data.json')
      .then(response => response.json())
      .then(result => {
        commit('setAll', result)
      })
    // console.log('child action')
    // commit('setAll', ['a', 'b'])
  },

  decrementInventory({commit}, id) {
    commit('changeAll', id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}